/** 
 * @author Huang RuiGuang
 * @date 2008/12/1
 *
 * Herm library
 * Copyright (c) 2008 by Huang RuiGuang(daemon.huang@gmail.com)
 *
 * Permission to use, copy, modify, distribute and sell this software.
 */

#ifndef HERM_FRAMEWORK_UDP_SESSION_MGR_H
#define HERM_FRAMEWORK_UDP_SESSION_MGR_H

#include "herm/base/container/obj_mgr.h"
#include "herm/base/network/udp_session.h"

namespace herm{
	class Address;
	struct Session;
}

typedef herm::SingleObjMgr<UDPSession> UDPSessionObjMgr;

class UDPPeer;
class UDPSessionMgr
{
public:
	UDPSessionMgr(UDPPeer* peer, const herm::PeerParam& peerParam);
	~UDPSessionMgr();

	UDPSession* Create(const herm::Address& addr);

	/**
	 * @brief Get a session by remote address.
	 * @return found, return this session; otherwise nullptr.
	 */ 
	UDPSession* Get(const herm::Address& addr);
	bool Destroy(herm::ISession* session);

	UDPSessionObjMgr* GetSessions() const;
private:
	typedef std::pair<herm::uint32_t, herm::uint16_t> AddrPair;
	struct AddrHash
	{
		int operator()(const AddrPair& ap, int capacity) const
		{
			return (ap.first + ap.second) & (capacity - 1);
		}
	};
//	typedef herm::HashTable<AddrPair, herm::uint32_t, AddrHash> AddrSessions;

	UDPPeer* m_peer;
	UDPSessionObjMgr* m_udpSessionObjMgr;
//	AddrSessions* m_addrSessions;
};

#endif

